package leetcode;

import java.util.*;

public class easy {
    public int[][] insert(int[][] intervals, int[] newInterval) {
        int left = newInterval[0];
        int right = newInterval[1];
        boolean isOps = false;
        List<int[]> res = new ArrayList<>();
        for (int[] ints : intervals) {
            if(ints[1] < left){
                res.add(ints);
            } else if (ints[0] > right) {
                if(!isOps){
                    res.add(new int[]{left,right});
                    isOps = true;
                }
                res.add(ints);
            }else{
                left = Math.min(ints[0],left);
                right = Math.max(ints[1],right);
            }
        }
        if(!isOps){
            res.add(new int[]{left,right});
        }
        return res.toArray(new int[res.size()][2]);
    }

    public static void main(String[] args) throws InterruptedException {
        easy easy = new easy();
        System.out.println(Arrays.deepToString(easy.insert(new int[][]{{1, 2}, {3, 5}, {6, 7}, {8, 10}, {12, 16}}, new int[]{4, 8})));
        System.out.println(Arrays.deepToString(easy.insert(new int[][]{{1,5}}, new int[]{2,7})));
    }
}
